#include"head.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
}


void QueuePush(Queue* pq, QueueData x)
{
	
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	newnode->x = x;
	newnode->next = NULL;
	if (pq->phead == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead == NULL;
}

void QueuePop(Queue* pq)
{
	assert(!QueueEmpty(pq));
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else 
	{
		QueueNode* pcur = pq->phead->next;
		free(pq->phead);
		pq->phead = pcur;
	}
	pq->size--;
}

QueueData QueueFront(Queue* pq)
{
	assert(!QueueEmpty(pq));
	return pq->phead->x;
}

QueueData QueueBack(Queue* pq)
{
	assert(!QueueEmpty(pq));
	return pq->ptail->x;
}

int QueueSize(Queue* pq)
{
	return pq->size;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* pcur;
	while (pq->phead != pq->ptail)
	{
		pcur = pq->phead->next;
		free(pq->phead);
		pq->phead = pcur;
	}
	free(pq->phead);
	pq->phead = pq->ptail = NULL;
}